package org.example.controller;


import org.example.model.Setting;
import org.example.model.User;
import org.example.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RequestMapping("/setting")
@RestController
public class SettingController {

    @Autowired
    private SettingService settingService;

    @GetMapping("/query")
    public Object query(@SessionAttribute("user") User user){
        //根据开发文档前后端接口定义，返回的data，就是我们Controller返回的对象
        //返回setting对象，还需要增加user,awards（还包含中奖人员id集合）,members字段
        Setting setting = settingService.query(user);
        return setting;
    }

    @GetMapping("/update")
    public Object update(Integer batchNumber,
                         @SessionAttribute("user") User user){//数据库里面就要把对应的设置给改掉所以加了后面这个参数
        //修改用户ID绑定对应用户ID的setting信息:都是根据非主键字段（user_id）修改非主键字段
        //Setting setting = new Setting();
        //做法一：自己写sql
        //做法二：使用用户生成工具已有的方法：先获取setting(根据用户id来查)，修改
        Setting setting = settingService.queryByUserID(user.getId());
        Setting updateSetting = new Setting();
        updateSetting.setId(setting.getId());
        updateSetting.setBatchNumber(batchNumber);
        int n = settingService.update(updateSetting);
        return null;
    }
}
